export default {
  namespaced: true,
  state: {
    type: 'all',
    list: JSON.parse(localStorage.getItem('vuexTodos')) || []
    // list: [
    //   {
    //     id: 100,
    //     name: "吃饭",
    //     isDone: true,
    //   },
    //   {
    //     id: 201,
    //     name: "睡觉",
    //     isDone: false,
    //   },
    //   {
    //     id: 103,
    //     name: "打豆豆",
    //     isDone: true,
    //   },
    // ],
  },
  mutations: {
    // 修改任务完成状态
    changeFn(state, id) {
      const item = state.list.find(item => item.id === id)
      item.isDone = !item.isDone
    },
    // 删除功能
    delFn(state, id) {
      state.list = state.list.filter(item => item.id != id)
    },
    // 添加功能
    add(state, name) {
      state.list.unshift({
        id: +new Date(),
        name,
        isDone: false,
      })
    },

    // 全选功能
    changeAll(state, val) {
      state.list.forEach(item => item.isDone = val)
    },


    // 修改type类型
    changeType(state, val) {
      state.type = val
    },

    // 清除已完成
    clearDone(state) {
      state.list = state.list.filter(item => !item.isDone)
    }

  },

  actions: {
    clearDoneAsync(context) {
      setTimeout(() => {
        context.commit('clearDone')
      }, 500)
    }
  },

  getters: {
    // 剩余数量值
    leftCount(state) {
      return state.list.filter(item => !item.isDone).length
    },

    // 清除已完成显示与隐藏
    showClear(state) {
      return state.list.some(item => item.isDone)
    },


    showList(state) {
      if (state.type === 'active') {
        return state.list.filter(item => !item.isDone)
      } else if (state.type === 'ok') {
        return state.list.filter(item => item.isDone)
      } else {
        return state.list
      }
    }
  }
}